//
//  BIDBillsOfType.m
//  1100163班第六组随手记
//
//  Created by Yeming on 13-6-11.
//
//

#import "BIDBillsOfType.h"
#import "BIDTypesManager.h"
#import "BIDABill.h"
#import "BIDClockVC.h"
#import "BIDBillsManager.h"
#import "BIDBillsLookoverManager.h"
@interface BIDBillsOfType ()
{
    NSInteger type_id;
    float spendMoney;
    float incomeMoney;
}
@end

@implementation BIDBillsOfType
@synthesize typeName,year,month;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   // NSLog(@"%d--%d--%@",month,year,typeName);
    self.title=typeName;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *bar=[[UIBarButtonItem alloc] initWithTitle:@"Delete" style:0 target:self action:@selector(delete:)];
   
    self.navigationItem.rightBarButtonItem=bar;
    [self.tableView reloadData];
   
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.navigationItem.rightBarButtonItem=nil;
}
-(void)delete:(id)sender
{
    UIBarButtonItem *bar=sender;
    if (!self.tableView.editing) {
        [bar setTitle:@"Done"];
        self.tableView.editing=YES;
    }
    else
    {
        [bar setTitle:@"Delete"];
        self.tableView.editing=NO;
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    type_id=[[BIDTypesManager shareTypesManager] selectTypeid:typeName];
    return [[BIDBillsLookoverManager shareBillsLookoverManager] listOfBillsCountOfMainType:type_id inMonth:month andYear:year];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
//    dispatch_queue_t que=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
//    dispatch_async(que, ^{
        BIDABill *abill=[[[BIDBillsLookoverManager shareBillsLookoverManager] listOfBillsOfMainType:type_id inMonth:month andYear:year] objectAtIndex:indexPath.row];
       NSString* typename=[[BIDTypesManager shareTypesManager] selectTypeName:abill.type_ID];
     NSString *    subType=[[BIDTypesManager shareTypesManager] selectSubTypeName:abill.subType_ID];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss "];
        NSDate *date = [dateFormatter dateFromString:abill.Ctime];
        
        [dateFormatter setDateFormat:@"dd"];
        NSString*timeStr=[dateFormatter stringFromDate:date];
//        dispatch_async(dispatch_get_main_queue(), ^{
            if (abill.whichWay==1) {
                 cell.textLabel.textColor=[UIColor redColor];
                spendMoney+=abill.Camount;
                
            }
            else
            {incomeMoney+=abill.Camount;
                
                cell.textLabel.textColor=[UIColor blackColor];
            }
             cell.textLabel.text=[NSString stringWithFormat:@"%@--%@ --%@日",typename,subType,timeStr ];
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

//        });
//    });
    // Configure the cell...
  
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    view.backgroundColor=[UIColor grayColor];
    UILabel *label=[[UILabel alloc] init];
    label.frame=CGRectMake(190, 0, 100, 30);
    // label.sizeToFit
    label.adjustsFontSizeToFitWidth=YES;
    label.adjustsLetterSpacingToFitWidth=YES;
    label.contentMode=UIViewContentModeScaleAspectFit;
    label.backgroundColor=[UIColor clearColor];
    label.textColor=[UIColor redColor];
    label.text=[NSString stringWithFormat:@"消费:%.2f",spendMoney];
    [view addSubview:label];
    [label release];
    spendMoney=0.0f;
    
    UILabel *label1=[[UILabel alloc] init];
    label1.frame=CGRectMake(40, 0, 100, 30);
    // label.sizeToFit
    label1.adjustsFontSizeToFitWidth=YES;
    label1.adjustsLetterSpacingToFitWidth=YES;
    label1.contentMode=UIViewContentModeScaleAspectFit;
    label1.backgroundColor=[UIColor clearColor];
    label1.textColor=[UIColor blackColor];
    label1.text=[NSString stringWithFormat:@"收入:%.2f",incomeMoney];
    [view addSubview:label1];
    incomeMoney=0.0f;
    [label1 release];

    return view;
    [view release];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0f;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
      BIDABill *abill=[[[BIDBillsLookoverManager shareBillsLookoverManager] listOfBillsOfMainType:type_id inMonth:month andYear:year] objectAtIndex:indexPath.row];
    [[BIDBillsManager shareBillsManager] deleteAPayout:abill];
    [self.tableView reloadData];
    
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    BIDABill *abill=[[[BIDBillsLookoverManager shareBillsLookoverManager] listOfBillsOfMainType:type_id inMonth:month andYear:year] objectAtIndex:indexPath.row];
//    NSString* typename=[[BIDTypesManager shareTypesManager] selectTypeName:abill.type_ID];
//    NSString *    subType=[[BIDTypesManager shareTypesManager] selectSubTypeName:abill.subType_ID];
    BIDClockVC *next=[[BIDClockVC alloc] initWithNibName:@"BIDClockVC" bundle:nil];
    next.aBill=abill;
    [self.navigationController pushViewController:next animated:YES];
}
@end
